﻿using FOModels.PersonalBusinessLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.PersonalBusinessLoan
{
    public class PersonalBusinessLoanDAL
    {
        private SQLDataAccess db;
        public PersonalBusinessLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from PersonalBusinessLoan order by PersonalBusinessLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from PersonalBusinessLoan sa join PersonalBusinessLoanLang hl on sa.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getPersonalBusinessLoanrelati(int CompanyID, int PersonalBusinessLoanID, int langid)
        {
            return db.exeSelect("select * from PersonalBusinessLoan ca join PersonalBusinessLoanLang hl on ca.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.PersonalBusinessLoanID != " + PersonalBusinessLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getPersonalBusinessLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from PersonalBusinessLoan sa join PersonalBusinessLoanLang hl on sa.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TablePersonalBusinessLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getPersonalBusinessLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from PersonalBusinessLoan sa join PersonalBusinessLoanLang hl on sa.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TablePersonalBusinessLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getPersonalBusinessLoanDefault(int langid)
        {
            return db.exeSelect("Select * from PersonalBusinessLoan sa join PersonalBusinessLoanLang hl on sa.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TablePersonalBusinessLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getPersonalBusinessLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from PersonalBusinessLoan sa join PersonalBusinessLoanLang hl on sa.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TablePersonalBusinessLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getPersonalBusinessLoanByID(int PersonalBusinessLoanID, int langid)
        {
            return db.exeSelect("select * from PersonalBusinessLoan ca join PersonalBusinessLoanLang hl on ca.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.PersonalBusinessLoanID = " + PersonalBusinessLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getPersonalBusinessLoanByIDManager(int PersonalBusinessLoanID, int langid)
        {
            return db.exeSelect("select * from PersonalBusinessLoan ca join PersonalBusinessLoanLang hl on ca.PersonalBusinessLoanID = hl.PersonalBusinessLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.PersonalBusinessLoanID = " + PersonalBusinessLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdatePersonalBusinessLoan(PersonalBusinessLoanModels models)
        {
            return db.exeUpdate("update PersonalBusinessLoan set PersonalBusinessLoanName = N'" + models.PersonalBusinessLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where PersonalBusinessLoanID = " + models.PersonalBusinessLoanID + "");
        }
        public int InsertPersonalBusinessLoan(PersonalBusinessLoanModels models)
        {
            return db.exeUpdate("insert into PersonalBusinessLoan values (N'" + models.PersonalBusinessLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeletePersonalBusinessLoan(int PersonalBusinessLoanID)
        {
            return db.exeUpdate("Delete from PersonalBusinessLoan where PersonalBusinessLoanID = " + PersonalBusinessLoanID + "");
        }
        public int UpdatePersonalBusinessLoanLang(PersonalBusinessLoanModels models, int langid)
        {
            return db.exeUpdate("update PersonalBusinessLoanLang set PersonalBusinessLoanName = N'" + models.PersonalBusinessLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where PersonalBusinessLoanID = " + models.PersonalBusinessLoanID + " and LangID=" + langid + "");
        }
        public int InsertPersonalBusinessLoanLang(PersonalBusinessLoanModels models, int langid)
        {
            return db.exeUpdate("insert into PersonalBusinessLoanLang values (" + models.PersonalBusinessLoanID + "," + langid + ",N'" + models.PersonalBusinessLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeletePersonalBusinessLoanLang(int PersonalBusinessLoanID)
        {
            return db.exeUpdate("Delete from PersonalBusinessLoanLang where PersonalBusinessLoanID = " + PersonalBusinessLoanID + "");
        }

        public int DeletePersonalBusinessLoanApply(int PersonalBusinessLoanApplyID)
        {
            return db.exeUpdate("Delete from PersonalBusinessLoanApply where PersonalBusinessLoanApplyID = " + PersonalBusinessLoanApplyID + "");
        }
        public DataTable getcontractbyid(int PersonalBusinessLoanapplyid)
        {
            return db.exeSelect("select * from PersonalBusinessLoanApply ca join [PersonalBusinessLoan] c on ca.PersonalBusinessLoanID = c.PersonalBusinessLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.PersonalBusinessLoanApplyID = " + PersonalBusinessLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from PersonalBusinessLoanApply ca join [PersonalBusinessLoan] c on ca.PersonalBusinessLoanID = c.PersonalBusinessLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from PersonalBusinessLoanApply ca join [PersonalBusinessLoan] c on ca.PersonalBusinessLoanID = c.PersonalBusinessLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int PersonalBusinessLoanApply(int PersonalBusinessLoanID, PersonalBusinessLoanApplyModels app)
        {
            return db.exeUpdate("insert into PersonalBusinessLoanApply(PersonalBusinessLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + PersonalBusinessLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(PersonalBusinessLoanApplyModels PersonalBusinessLoan)
        {
            return db.exeUpdate("update PersonalBusinessLoanApply set StatusID= " + PersonalBusinessLoan.StatusID + " where PersonalBusinessLoanApplyID = " + PersonalBusinessLoan.PersonalBusinessLoanApplyID + "");
        }
    }
}
